<template>
  <section id="main-content">
    <section class="wrapper">
      <div class="row fanmargin">
        <div class="col-lg-12">
          <h4 class="myh4"><i class="fa fa-angle-right"></i>  添加盘管</h4>
          <div class="form-panel">
            <div class=" form">
              <form class="cmxform form-horizontal style-form">
                <div class="form-group ">
                  <label for="cname" class="control-label col-lg-2 fanfont">编号</label>
                  <div class="col-lg-10">
                    <input class=" form-control" v-model.trim="fanNumber" type="number" required="" autofocus>
                  </div>
                </div>
                <div class="form-group ">
                  <label for="cemail" class="control-label col-lg-2 fanfont">楼层</label>
                  <div class="col-lg-10">
                    <input class="form-control" v-model.trim="floor" type="number" required="">
                  </div>
                </div>
                <div class="form-group ">
                  <label for="curl" class="control-label col-lg-2 fanfont">房间</label>
                  <div class="col-lg-10">
                    <input class="form-control" v-model.trim="room" type="text" required="">
                  </div>
                </div>
                <div class="form-group ">
                  <label for="ccomment" class="control-label col-lg-2 fanfont">描述</label>
                  <div class="col-lg-10">
                    <textarea class="form-control" v-model.trim="information"></textarea>
                  </div>
                </div>
                <div class="form-group ">
                  <label for="ccomment" class="control-label col-lg-2 fanfont">采集器</label>
                  <div class="col-lg-10">
                    <select class="form-control" v-model.trim="collectId">
                      <option :value="collect.id" v-for="collect in collectList" >{{collect.collectNumber}}</option>
                    </select>
                  </div>
                </div>
                <div class="form-group">
                  <div class="col-lg-offset-2 col-lg-10">
                    <button class="btn btn-theme"  v-bind:disabled="isReflash" type="button" @click="submit">保存</button>
                    <button class="btn btn-theme04" type="button" @click="cancel">返回</button>
                  </div>
                </div>
              </form>
            </div>
          </div>
        </div>
      </div>
    </section>
  </section>
</template>

<script>
  var numReg = /^[0-9]+$/
  var numRe = new RegExp(numReg)
  export default {
    name: 'addfan',
    data () {
      return {
        isReflash:false,
        collectList:[],
        fanNumber:'',
        information:'',
        floor:'',
        room:'',
        collectId:'',
      }
    },
    mounted () {
      this.getcollectList()
    },
    methods:{
      cancel:function(){
        this.$emit('isadd', true)
      },
      submit:function(){
        var token = sessionStorage.getItem('shiro-token')
        var schoolId = sessionStorage.getItem('schoolId')
        if(!schoolId){
          this.showToast("学校不存在")
          return false
        }
        if(!this.collectId){
          this.showToast("请选择采集器")
          return false
        }
        if(!numRe.test(this.fanNumber)){
          this.showToast("编号只能输入数字")
          return false
        }
        if(!numRe.test(this.floor)){
          this.showToast("编号只能输入数字")
          return false
          return false
        }
        if(!this.room){
          this.showToast("请输入房间名称或编号")
          return false
        }
        this.isReflash = true
        var that = this
        var instance = this.$axios.create({
            headers: {'Authorization':token}
        });
        instance.post('ganzi-server/school/addFan',{
          schoolId:schoolId,
          collectId:this.collectId,
          fanNumber:this.fanNumber,
          information:this.information,
          floor:this.floor,
          room:this.room
        })
        .then(function (response) {
          that.showToast(response.data.msg)
          that.clear()
          that.isReflash = false
        })
        .catch(function (error) {
          if(error.response && error.response.data.status == 4001){
            sessionStorage.clear();
            that.$router.push({path: '/'})
          }else if(error.response && error.response.data.status == 4002){
            sessionStorage.clear();
            that.$options.methods.showToast('您的账号在其他地方登录,如非本人操作，请立即修改密码！')
            that.$router.push({path: '/'})
          }else{
            that.$options.methods.showToast('系统异常!')
          }
          that.isReflash = false
        });
        return true
      },
      getcollectList:function(){
        var that = this
        var token = sessionStorage.getItem('shiro-token')
        var schoolId = sessionStorage.getItem('schoolId')
        var instance = this.$axios.create({
            headers: {'Authorization':token}
        });
        instance.post('ganzi-server/info/fanCollectInformation',{
          schoolId:schoolId
        })
        .then(function (response) {
          if(response.data.code == '200'){
              if(response.data.data && response.data.data.length>0 ){
                that.collectList = response.data.data
                if(that.collectList.length>0){
                  that.collectId = that.collectList[0].id
                }
              }
          }else{
            that.showToast(response.data.msg)
          }
        })
        .catch(function (error) {
          if(error.response && error.response.data.status == 4001){
            sessionStorage.clear();
            that.$router.push({path: '/'})
          }else if(error.response && error.response.data.status == 4002){
            sessionStorage.clear();
            that.$options.methods.showToast('您的账号在其他地方登录,如非本人操作，请立即修改密码！')
            that.$router.push({path: '/'})
          }else{
            that.$options.methods.showToast('系统异常!')
          }
        });
      },
      showToast:function(msg){
        $.alert({
          title: '消息!',
          content: msg,
          confirmButton: '确定',
          confirmButtonClass: 'btn-warning',
          backgroundDismiss: false,
          confirm: function(){
          }
        });
      },
      clear:function(){
        this.fanNumber = ''
        this.information = ''
        this.floor = ''
        this.room = ''
      }
    }
  }
</script>

<style scoped>
  @import "../assets/css/school.css";
</style>
